//
//  STCollectionViewCell.m
//  STTools
//
//  Created by stoneobs on 16/10/13.
//  Copyright © 2016年 stoneobs. All rights reserved.
//

#import "STBWCollectionViewCell.h"


@interface STBWCollectionViewCell()<UIGestureRecognizerDelegate,UIScrollViewDelegate>

@end
@implementation STBWCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundImageView =  [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, STBW_SCREEN_WIDTH, STBW_SCREEN_HEIGHT)];
        [self addSubview:self.backgroundImageView];
        UIVisualEffectView * view = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
        view.frame = self.backgroundImageView.bounds;
        self.backgroundImageView.contentMode = UIViewContentModeScaleAspectFill;
        self.backgroundImageView.clipsToBounds = YES;
        [self.backgroundImageView addSubview:view];
        
        _scrollView = [[UIScrollView alloc]initWithFrame:STBW_SCREEN_FRAME];
        _scrollView.backgroundColor = [UIColor clearColor];
        _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _scrollView.scrollEnabled = YES;
        _scrollView.delegate = self;
        [_scrollView setMinimumZoomScale:1];
        [_scrollView setMaximumZoomScale:3.0];
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, STBW_SCREEN_WIDTH, STBW_SCREEN_HEIGHT)];
        self.imageView.center = [UIApplication sharedApplication].keyWindow.center;
        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
        self.imageView.backgroundColor = [UIColor clearColor];
        self.imageView.userInteractionEnabled = YES;
        self.imageView.transform = CGAffineTransformIdentity;
        [_scrollView addSubview:self.imageView];
        [self addSubview:_scrollView];
    }
    return self;
}

-(void)setModel:(STBWPhotoModel *)model
{
    __weak typeof(self) weakSelf =  self;
    if (model) {
        _model = model;
        self.backgroundImageView.hidden = model.shouldHideVisualEffectView;
        [_scrollView setZoomScale:1 animated:YES];
        //点击
        [self.imageView removeGestureRecognizer:_tapGes];
        _tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
        _tapGes.delegate = self;
        _tapGes.enabled = YES;
        [self.imageView addGestureRecognizer:_tapGes];
        
        if (model.originImage) {
            self.imageView.image = model.originImage;
            self.backgroundImageView.image = model.originImage;
            [self updateImageView];
            return;
        }
        if (model.thumbImage) {
            self.imageView.image = model.thumbImage;
            self.backgroundImageView.image = model.thumbImage;
            [self updateImageView];
            return;
        }
        if (model.originImageUrl) {
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.originImageUrl] placeholderImage:model.placeHolderImage  completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                model.thumbImage = image;
                [self updateImageView];
                weakSelf.backgroundImageView.image = image;
            }];
            
            return;
        }
        
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.thumbImageUrl] placeholderImage:model.placeHolderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            model.thumbImage = image;
            [self updateImageView];
            weakSelf.backgroundImageView.image = image;
        }];
        
        return;
        
    }
    
}
- (void)updateImageView{
    UIImage * image = self.imageView.image;
    if (!image) {
        return;
    }
    CGFloat bilie = image.size.height / image.size.width;
    CGSize dealSize = CGSizeMake(STBW_SCREEN_WIDTH, STBW_SCREEN_WIDTH* bilie);
    _imageView.stbw_size = dealSize;
    _imageView.center = UIApplication.sharedApplication.delegate.window.center;
}

- (void)tapGesture:(UITapGestureRecognizer*)sender{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(didClicTheImageView:cell:)]) {
        [self.delegate didClicTheImageView:(id)sender.view cell:self];
    }
}

//将要remove 的时候将所有还原
-(void)backToOrigin
{
    self.scrollView.zoomScale = 1.0;
    [self updateImageView];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    if (self.imageView.image) {
        return  self.imageView;
    }
    return nil;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    CGFloat offsetX = (self.scrollView.bounds.size.width>self.scrollView.contentSize.width)?(self.scrollView.bounds.size.width-self.scrollView.contentSize.width)*0.5:0.0;
    CGFloat offsetY = (self.scrollView.bounds.size.height>self.scrollView.contentSize.height)?(self.scrollView.bounds.size.height-self.scrollView.contentSize.height)*0.5:0.0;
    _imageView.center = CGPointMake(scrollView.contentSize.width*0.5+offsetX, scrollView.contentSize.height*0.5+offsetY);
    NSLog(@"ing%f frame:%@",scrollView.zoomScale,NSStringFromCGRect(self.imageView.frame));
    if (self.delegate && [self.delegate respondsToSelector:@selector(didZoomScale:cell:imageView:)]) {
        [self.delegate didZoomScale:scrollView.zoomScale cell:self imageView:self.imageView];
    }
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale{
    NSLog(@"end_%f",scale);
}
@end

